<template>
    <div class="wrap">
        <div class="wrap-content">
            <instance-tree :treeData="instanceTree" @currentNodeData="getCurrentInstanceInfo"
                           class="instance-component"></instance-tree>
            <part-doc-material-info :partMaterialData="currentSelectNodeMaterialData"
                                    :currentPartDocInfo="currentSelectNodeInsInfo"
                                    :addDisabled="addDisabled"
                                    class="material-component">
                <template #cancelButton>
                    <el-button type="primary" size="small"
                               @click="cancel">{{ $t('message.common.cancel') }}
                    </el-button>
                </template>
            </part-doc-material-info>
        </div>
    </div>
</template>

<script>
import DialogMixin from "../../DialogMixin";
import {materialSettingCommand} from "../../../../command/MaterialSettingCommand";
import InstanceTree from "./InstanceTree.vue";
import PartDocMaterialInfo from "../PartDocMaterialSetting/PartDocMaterialInfo.vue";

export default {
    name: "AssemblyDocMaterialSetting",
    mixins: [DialogMixin],
    components: {PartDocMaterialInfo, InstanceTree},
    data() {
        return {
            addDisabled:true,
            isInit:false,
            instanceTree: [],
            currentSelectNodeInsInfo: [],//当前选择节点文档信息
            currentSelectNodeMaterialData: [],//当前选择节点材料信息
        }
    },
    mounted() {
        if (!this.isInit) {
            materialSettingCommand.init(1300, 1275, 1100, () => {
                materialSettingCommand.clearAllHighLight();
                //获取InstanceTree
                this.getInstanceTree();
                this.isInit = true;
            })
        }
    },
    methods: {
        changePluginContainerHeight() {
            materialSettingCommand.changePluginContainerHeight({
                minWidth: this.$el.clientWidth,
                maxHeight: this.$el.clientHeight,
                width: 1275
            })
        },
        cancel() {
            materialSettingCommand.closeActiveCmd();
        },
        getInstanceTree() {
            materialSettingCommand.getInstanceTree(res => {
                //获取当前装配文件的id,name,children
                this.instanceTree = res;
            })
        },
        //获取当前选择Node的实例信息
        async getCurrentInstanceInfo(data) {
            this.addDisabled = true;
            let id = data.id;
            this.currentSelectNodeInsInfo = []
            let res = await materialSettingCommand.getCurrentInstanceInfo(id);
            this.currentSelectNodeInsInfo = res;
            if (res) {
                if (res?.protoRefType === 'assembly') {
                    this.currentSelectNodeMaterialData = [];
                } else if (res?.protoRefType === 'part') {
                    this.addDisabled = false;
                    let docId = res?.protoRefDocId;
                    materialSettingCommand.getCurrentPartMaterialInfo(docId, res => {
                        this.currentSelectNodeMaterialData = res ?? [];
                    })
                }
            }
        },
    },
}
</script>

<style scoped>
.wrap {
    font-size: 12px;
    height: 590px;
}

.wrap-content {
    color: var(--text-color1);
    font-size: var(--text-14);
    display: flex;
    flex-grow: 1;
    height: 100vh;
    max-height: 1000px;
    max-width: 1800px;
}

.instance-component {
    width: 210px;
    height: 100%;
    border: 2px solid var(--bg-color3);
    padding: 8px;
    text-overflow: ellipsis;
    overflow-y: auto;
}

.material-component {
    border: 2px solid var(--bg-color3);
    padding: 8px;
    height: 100%;
}

.dialog-footer {
    margin-left: 15px;
}
</style>